﻿using System;
using System.Net;
using System.Windows;
using System.Collections.Generic;
using TestApp.Model;
using TestApp.WebService;
using TestApp.Resources;
using System.Threading.Tasks;

namespace TestApp.Model.Managers
{
    public sealed class CouponsManager
    {
        private static volatile CouponsManager instance;
        private static object syncRoot = new Object();

        private CouponsManager() { }

        // Singleton Instance
        public static CouponsManager Instance
        {
            get 
            {
                 if (instance == null) 
                 {
                    lock (syncRoot) 
                    {
                       if (instance == null)
                           instance = new CouponsManager();
                    }
                 }

                 return instance;
           }
        }

        // Coupons Property
        public List<Coupon> Coupons
        {
            get;
            set;
        }

        // Gets the Coupons using the CouponsService
        public async Task<List<Coupon>> FetchCoupons()
        {
            
            Coupons = await CouponsService.Instance.FetchCoupons().ConfigureAwait(false);
            return Coupons;
        }

        // Returns a list of CouponCategories for a list of Coupons
        public List<CouponCategory> CategoriesForCoupons(List<Coupon> coupons)
        {
            List<CouponCategory> categories = new List<CouponCategory>();
            CouponCategory allCategories = new CouponCategory();

            allCategories.CategoryId = @"-1";
            allCategories.Name = LocalizedStrings.CouponCategoriesAll;

            categories.Add(allCategories);

            foreach (Coupon coupon in coupons)
            {
                if (!categories.Contains(coupon.Category))
                {
                    categories.Add(coupon.Category);
                }
            }
            return categories;
        }
    }
}
